<?php

class Db{
    
    public $query;
    public $row;
    private $result;
    private $conexion;
        
    function __construct(){
        
        require_once $_SERVER['DOCUMENT_ROOT'] . '/gestion/includes/db.cfg.php';
        
    }
    
    public function ejecutaSP($numero, $sp, $parametro){
        
        $this->result = pg_query("SELECT ". $sp ."($parametro)");
        
        $this->error($numero);
        
    }
    
    public function spResultado($numero, $sp, $parametro){
        
        $this->ejecutaSP($numero, $sp, $parametro);
        
        # Seteamos el resultado
        $this->convierteArray();
        
    }


    public function ejecutaQuery($numero){
        
        $this->result = $this->conexion->query($this->query);
        
        $this->error($numero);
                      
    }
    
    public function retornaResultado($numero){
        
        $this->ejecutaQuery($numero);
        
        $this->convierteArray();
        
    }
    
    public function convierteArray(){
        
        $this->row = '';
        
        if ($this->result->num_rows > 0){
            
            while ($this->row[] = $this->result->fetch_assoc());
            
            if (is_array($this->row) && count($this->row) > 1){
            
                array_pop($this->row);
            
            }
            
        }
        
    }
    
    public function liberaResult(){
        
        //$this->conexion->
        
    }


    private function error($numero){
        
        if ($this->conexion->error){
            
            echo "Error $numero, proceso interrumpido.<br />";
            
            exit();
            
        }
        
    }
    
}

?>